<template>
  <div>
    <el-card>
      <el-tabs value="first">
        <el-tab-pane label="登录账户设置" name="first">
          <el-form
            ref="userRef"
            label-width="120px"
            style="margin-left: 120px; margin-top:30px"
            :model="userInfo"
            :rules="rules"
          >
            <el-form-item label="姓名:" prop="username">
              <el-input v-model="userInfo.username" style="width:300px" />
            </el-form-item>
            <el-form-item label="密码:" prop="password">
              <el-input v-model="userInfo.password" style="width:300px" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="saveUser">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <component :is="userComponent"></component>
          <!-- <user-info></user-info> -->
        </el-tab-pane>
        <el-tab-pane label="角色管理" name="third">
          <component :is="jobComponent"></component>
        </el-tab-pane>

      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import JobInfo from './components/job-info'
import UserInfo from './components/user-info.vue'
import { getUserDetaiInfoById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  name: 'EmployeesDetail',
  components: {
    UserInfo,
    JobInfo
  },
  props: {},
  data() {
    return {
      jobComponent: JobInfo,
      userComponent: UserInfo,
      userId: this.$route.params.id, // 拿到路由保存的用户id
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUserDetaiInfoById()
  },
  methods: {
    async getUserDetaiInfoById() {
      const userInfo = await getUserDetaiInfoById(this.userId)
      this.userInfo = { ...userInfo, password: '' } // 将密码框手动清空 因为服务器返回过来的是加密字符串
    },
    async saveUser() {
      try {
        await this.$refs.userRef.validate()
        await saveUserDetailById(this.userInfo)
        this.$message.success('修改成功!')
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>

<style lang="less" scoped>

</style>
